package com.cube.dp.base.error;

import com.cube.dp.base.response.ApiResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author cube.li
 * @date 2021/12/5 16:31
 * <p>
 * 全局异常处理器
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(CommonBuException.class)
    public ApiResult<Void> customExceptionHandler(CommonBuException ce) {
        return ApiResult.error(ce.getCode(), ce.getMessage());
    }

    @ExceptionHandler(NullPointerException.class)
    public ApiResult<Void> nullPointException(NullPointerException nullPointerException) {
        return ApiResult.error(CommonApiResultCode.NULL_POINT.getCode(), CommonApiResultCode.NULL_POINT.getMsg());
    }

    @ExceptionHandler(Exception.class)
    public ApiResult<Void> exceptionHandler(Exception e) {
        log.error("系统异常,error_msg = {}", e.getMessage());
        return ApiResult.error(CommonApiResultCode.SYSTEM_ERROR.getCode(), CommonApiResultCode.SYSTEM_ERROR.getMsg());
    }
}
